// app.js
import request from "utils/request"
import {store} from "utils/store"
import {config,constVAR} from "config"
import {User} from "utils/dao"
App({
  onLaunch(options) {
    // 展示本地存储能力
    let data = store.get(constVAR.KEY_APP)
    if(data == undefined) {
      data = User
      console.log(data)
    } else {
      data = JSON.parse(data)
    }
    const channel = options.query.name
    if(data.channel != channel) {
      data.channel = channel
    }
    this.globalData.user = data
    // 登录
    wx.login()
      .then(res => { //登录
        if(res.code) {
            return request.post(config.route.login, {code: res.code,channel: channel})
        } else {
            wx.showToast({title: '小程序需要您的授权才能访问呢'})
        }
      })
      .then(res => {
        this.globalData.user.openid = res.data.openid
        this.globalData.user.cover = res.data.cover
        this.globalData.user.background = res.data.background
        this.globalData.user.agent_bot = res.data.bot_id
        this.globalData.user.agent_icon = res.data.icon
        this.globalData.user.agent_name = res.data.name
        this.globalData.user.know = res.data.know
        this.globalData.user.name = res.data.name
        store.put("app", JSON.stringify(this.globalData.user))
      })
  },
  globalData: {
    userInfo: null
  }
})
